package slidingWindows;

public class totalFruit {
    public int totalFruit(int[] f) {
        int n=f.length;
        int[] hash=new int[n+1];
        int ret=0;
        for(int left=0,right=0,kind=0;right<n;right++){
            int in=f[right];
            if(hash[in]==0) kind++;
            hash[in]++;
            while(kind>2){
                int out=f[left];
                hash[out]--;
                if(hash[out]==0)kind--;
                left++;
            }
            ret=Math.max(ret,right-left+1);
        }
        return ret;
    }
}
